package com.lwj.mallchat.common.common.event.listener;

import com.lwj.mallchat.common.common.event.UserRegisterEvent;
import com.lwj.mallchat.common.user.dao.UserDao;
import com.lwj.mallchat.common.user.domain.entity.User;
import com.lwj.mallchat.common.user.domain.enums.IdempotentEnum;
import com.lwj.mallchat.common.user.domain.enums.ItemEnum;
import com.lwj.mallchat.common.user.service.UserBackpackService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;

/**
 * @author lwj
 * @ClassName UserRegisterListener
 * @date 2024/10/22 11:03
 */
@Component
public class UserRegisterListener {

    @Autowired
    private UserBackpackService userBackpackService;

    @Autowired
    private UserDao userDao;

    @Async
    @TransactionalEventListener(classes = UserRegisterEvent.class, phase = TransactionPhase.AFTER_COMMIT)
    public void sendCard(UserRegisterEvent registerEvent) {
        //注册送改名卡
        User user = registerEvent.getUser();
        userBackpackService.acquireItem(user.getId(), ItemEnum.MODIFY_NAME_CARD.getId(), IdempotentEnum.UID, user.getId().toString());
    }

    @TransactionalEventListener(classes = UserRegisterEvent.class, phase = TransactionPhase.AFTER_COMMIT)
    public void sendBadge(UserRegisterEvent registerEvent) {
        //送徽章
        User user = registerEvent.getUser();
        Integer count = userDao.lambdaQuery().count();
        if (count<10){
            userBackpackService.acquireItem(user.getId(), ItemEnum.REG_TOP10_BADGE.getId(), IdempotentEnum.UID, user.getId().toString());
        }else if (count<100){
            userBackpackService.acquireItem(user.getId(), ItemEnum.REG_TOP100_BADGE.getId(), IdempotentEnum.UID, user.getId().toString());
        }else{

        }

    }
}
